﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.BaseDoc.Model;
using SmartWeb.BLL;
using System.IO;
using Yipusoft.BaseDoc.BLL;
using System.Data;

public partial class Task_BaseDoc_DocDepartmentResult : System.Web.UI.Page
{
    List<DocumentFileInfo> Glist;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            Bind();

        }
    }

    private void Bind()
    {
        List<DocumentFileInfo> list = new List<DocumentFileInfo>();
        string where = "";
        if (Request["IDs"] != null)
        {
            Glist = new List<DocumentFileInfo>();
            string IDs = Request["IDs"];
            where = " ID in (" + IDs + ")";

        }
        if (Request["Key"] != null)
        {
            Glist = new List<DocumentFileInfo>();
            string Key = Request["Key"];
            string StartDate = Request["Year"] + "-" + Request["SM"] + "-1";
            string EndDate = Request["Year"] + "-" + Request["EM"] + "-1";
            EndDate = Convert.ToDateTime(EndDate).AddMonths(1).ToString();
            where = " oldFileName like '%" + Key + "%' and departmentid='" + Request["DepartmentID"] + "' and (StartDate>='" + StartDate + "') and (StartDate<'" + EndDate + "')";
            if (Request["ParentID"] != null && Request["ParentID"] != "")
            {
                where = " ParentID='" + Request["ParentID"] + "' and " + where;
            }
        }

        DataTable dt = FileManager.GetMonthFileCount(where);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DocumentFileInfo info = new DocumentFileInfo();
            info.Level = 0;
            info.OldFileName = Convert.ToString((CNum)dt.Rows[i][0]) + "月份";
            info.Format = "";
            info.Num = -1;
            info.IsMonth = true;
            info.IsFolder = true;
            info.NewFileName = Convert.ToInt32(dt.Rows[i][0]).ToString();
            info.Month = Convert.ToInt32(dt.Rows[i][0]);
            info.IsFolder = true;
            info.OldFileName = " <img src= \"../../Images/ico/book.gif\"/>&nbsp;&nbsp;" + info.OldFileName;
            Glist.Add(info);
            GetMonthFile(Convert.ToInt32(dt.Rows[i][0]));
        }
        gvwFileList.DataSource = Glist;
        ListPageContral1.ItemCount = Glist.Count();
        gvwFileList.DataBind();
    }

    private void GetMonthFile(int i)
    {


        string StartDate = Request["Year"] + "-" + i + "-1";
        string EndDate = Convert.ToDateTime(StartDate).AddMonths(1).ToString();
        string DepartmentID = Request["DepartmentID"];

        string where = " DepartmentID='" + DepartmentID + "' and (StartDate>='" + StartDate + "') and (StartDate<'" + EndDate + "')";
        if (Request["IDs"] != null)
        {
            //Glist = new List<DocumentFileInfo>();
            string IDs = Request["IDs"];
            where += " and ID in (" + IDs + ")";

        }
        if (Request["Key"] != null)
        {
            // Glist = new List<DocumentFileInfo>();
            string Key = Request["Key"];
            where += " and OldFileName like '%" + Request["Key"] + "%'";
            if (Request["ParentID"] != null && Request["ParentID"] != "")
            {
                where = " ParentID='" + Request["ParentID"] + "' and " + where;
            }
        }


        where += "Order by Level, OldFileName";
        List<DocumentFileInfo> list = Yipusoft.BaseDoc.BLL.FileManager.GetList(where);
        int mun = 1;
        foreach (DocumentFileInfo info in list)
        {
            info.Num = mun;
            mun++;

            info.OldFileName = " <img src= \"../../Images/ico/" + info.Format + ".gif\" />&nbsp;&nbsp;" + info.OldFileName;
            var pp = from o in Glist where o.ID == info.ID select o;
            if (pp.Count() == 0)
            {
                Glist.Add(info);
            }
            OrderItem(info.ID);
        }
    }
    private void OrderItem(Guid ID)
    {

        string FileCountwhere = "  ParentID='" + ID + "' Order by OldFileName ";
        string where = string.Empty;

        where = "  ParentID='" + ID + "'  Order by OldFileName ";


        List<Yipusoft.BaseDoc.Model.DocumentFileInfo> list = Yipusoft.BaseDoc.BLL.FileManager.GetList(where);
        int mun = 1;
        if (list != null)
        {
            foreach (DocumentFileInfo info in list)
            {
                info.OldFileName = " <img src= \"../../Images/ico/" + info.Format + ".gif\" />" + info.OldFileName;
                info.Num = mun;
                mun++;
                var pp = from o in Glist where o.ID == info.ID select o;
                if (pp.Count() == 0)
                {
                    Glist.Add(info);
                }
                OrderItem(info.ID);
            }
        }
        list = Yipusoft.BaseDoc.BLL.FileManager.GetList(FileCountwhere);
        //if (list != null)
        //{
        //    foreach (DocumentFileInfo info in list)
        //    {
        //        if (!info.IsFolder)
        //        {
        //            lblDocSum.Text = Convert.ToString(Convert.ToInt32(lblDocSum.Text) + 1);
        //        }
        //    }
        //}
    }
    public string GetChildCount(object objID)
    {
        if (objID == null)
            return "0";
        int temp = Yipusoft.BaseDoc.BLL.FileManager.GetChildFileCount((Guid)objID, true);
        DocumentFileInfo info = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(objID.ToString()));
        if (info == null)
        {
            return "";
        }
        if (Yipusoft.BaseDoc.BLL.FileManager.GetFile((Guid)objID).ParentID.Equals(Guid.Empty))
            return "<font color=red>" + temp + "</font>";
        else
            return temp.ToString();

    }

    public string GetChar(object objID)
    {
        DocumentFileInfo info = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(objID.ToString()));
        if (info == null)
        {
            return "";
        }
        else
        {
            return "".PadLeft(2 * info.Level.Value, '　');
        }
    }
    protected void gvwFileList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btn = e.Row.FindControl("LinkButton1") as LinkButton;
            if (btn == null)
                return;
            //btn.PostBackUrl = "DocDepartmentList.aspx?ID=" + btn.CommandArgument;
            DocumentFileInfo fileinfo = e.Row.DataItem as DocumentFileInfo;
            if (fileinfo.IsMonth)//月份
            {

                LinkButton LinkButton1 = e.Row.FindControl("LinkButton1") as LinkButton;
                LinkButton1.CommandName = "mt";
                LinkButton1.CommandArgument = fileinfo.Month.ToString();
                LinkButton1.ToolTip = fileinfo.OldFileName;
                e.Row.BackColor = System.Drawing.Color.FromName("#F5FBF9");
            }
        }
    }
    protected void gvwFileList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "re")
        {
            DocumentFileInfo info = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(e.CommandArgument.ToString()));
            if (info == null)
            {

            }
            else
            {
                if (info.Format == "Folder")
                    Response.Redirect("DocDepartmentList1.aspx?Year=" + info.StartDate.Value.Year + "&SM=" + info.StartDate.Value.Month + "&EM=" + info.StartDate.Value.Month + "&ID=" + e.CommandArgument.ToString() + "&DepartmentID=" + Request["DepartmentID"]);
                else
                    DownLoad(info.NewFileName, info.OldFileName, info.FilePath);
            }
        }
        if (e.CommandName == "mt")//月份操作
        {
            Response.Redirect("DocDepartmentList.aspx?DepartmentID=" + Request["DepartmentID"] + "&Year=" + Request["Year"] + "&SM=" + e.CommandArgument + "&EM=" + e.CommandArgument);
        }
    }

    private void DownLoad(string name, string oldFileName, string filePath)
    {
        string fileName = name;//客户端保存的文件名       

        FileInfo fileInfo = new FileInfo(Server.MapPath(filePath));
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();

    }
    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        gvwFileList.PageIndex = PageIndex;
        Bind();
    }

    public string GetMonth(object Date)
    {
        DateTime date = Convert.ToDateTime(Date);
        if (date != null)
        {
            return Convert.ToString((CNum)date.Month) + "月份";
        }
        else
        {
            return "";
        }
    }
}
